package com.example.Controller;

import com.example.Entity.UserInfo;
import com.example.Service.Impl.UserInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Controller
@Api(tags = "用户模块")
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    UserInfoServiceImpl userInfoService;

    @PostMapping(value = "/register")
    @ResponseBody
    @ApiOperation(value = "用户注册功能")
    private boolean userRegister(@RequestBody String req) {
        JSONObject jsonObject = JSONObject.fromObject(req);
        String account = jsonObject.getString("account");
        String userName = jsonObject.getString("userName");
        String password = jsonObject.getString("password");
        String mobileBox = jsonObject.getString("mobileBox");
        String cloudLink = jsonObject.getString("cloudLink");
        String cloudKey = jsonObject.getString("cloudKey");
        UserInfo user = new UserInfo();
        user.setUserName(userName);
        user.setAccount(account);
        user.setPassword(password);
        user.setMobileBox(mobileBox);
        user.setCloudLink(cloudLink);
        user.setCloudKey(cloudKey);
        try {
            userInfoService.save(user);
        } catch (Exception E) {
            E.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @param account  账号（手机号）
     * @param password 密码
     * @return 成功或是失败的页面，这里只要有一个用户登录了那么服务器就会存在session，那么所有人都能访问了；
     * 这里并不能进行拦截，因为LoginHandlerInterceptor返回的只是false或者true
     */
    @PostMapping("/userInput")
    @ResponseBody
    @ApiOperation(value = "用户登录功能")
    public boolean userInput(String account, String password) {
//        session.setAttribute("loginUser",account);
//        session.invalidate();
        return userInfoService.userInput(account, password);
    }
//    @GetMapping("/userOut")
//    @ResponseBody
//    @ApiOperation(value = "注销登录功能")
//    public boolean userOut(HttpSession session){
//        session.invalidate();
//        return true;
//    }
//}
}
